package com.yb.bussiness.information;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.yb.base.dao.BaseDao;
import com.yb.base.dao.IBaseDao;
import com.yb.domain.goods.TgGoods;
import com.yb.domain.goods.TgInformation;
import com.yb.domain.goods.TgInformationFormBean;
import com.yb.util.comm.DateTimeUtil;
import com.yb.util.framework.PageUtil;

public class InformationServiceImpl implements InformationService {
    
    private IBaseDao<TgInformation, Long> dao;
    
    public InformationServiceImpl() {
        dao = new BaseDao<TgInformation, Long>(TgInformation.class);
    }
    
    public boolean deleteByIdString(String idString) {
        boolean isFlag = true;
        if (idString == null || "".equals(idString))
            return false;
        String sql = "delete from t_g_information where id in (" + idString + ")";
        try {
            dao.delete(sql);
        }
        catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return isFlag;
    }
    
    public Map<String, Object> findAll(TgInformationFormBean formBean, String page, String row) {
        String sql =
            "select n.id,n.title,n.hotActivities,c.name as content,(select name from t_g_category where n.pcategoryid = id) as ImgUrl,n.isRecommend, n.createDate,n.hot,n.ImgFlag,n.weights  from t_g_information n left join t_g_category c on n.categoryid = c.id where 1=1 ";
        String countSql = "select count(n.id) from t_g_information n where 1=1 ";
        if (formBean != null) {
            if (formBean.getTitle() != null && !"".equals(formBean.getTitle())) {
                sql += " and n.title like '%" + formBean.getTitle() + "%' ";
                countSql += " and n.title like '%" + formBean.getTitle() + "%' ";
            }
            if (formBean.getPcategoryId() != null && !"".equals(formBean.getPcategoryId())) {
                sql += " and n.pcategoryid = " + formBean.getPcategoryId();
                countSql += " and n.pcategoryid = " + formBean.getPcategoryId();
            }
            if (formBean.getCategoryId() != null && !"".equals(formBean.getCategoryId())) {
                sql += " and n.categoryId = " + formBean.getCategoryId();
                countSql += " and n.categoryId = " + formBean.getCategoryId();
            }
//            if (formBean.getPcategoryId() != null && !"".equals(formBean.getPcategoryId())) {
//                sql += " and n.PcategoryId = " + formBean.getPcategoryId();
//                countSql += " and n.PcategoryId = " + formBean.getPcategoryId();
//            }
            if ("1".equals(formBean.getIsRecommend())) {
                sql += " and n.isRecommend = " + formBean.getIsRecommend();
                countSql += " and n.isRecommend = " + formBean.getIsRecommend();
            }
            if ("2".equals(formBean.getIsRecommend())) {
                sql += " and n.isRecommend is null ";
                countSql += " and n.isRecommend is null ";
            }
            if (formBean.getHotFlag() != null && !"".equals(formBean.getHotFlag())) {
                sql += " and n.hot = " + formBean.getHotFlag();
                countSql += " and n.hot = " + formBean.getHotFlag();
            }
            if (formBean.getImgFlag() != null && !"".equals(formBean.getImgFlag())) {
                sql += " and n.imgflag = " + formBean.getImgFlag();
                countSql += " and n.imgflag = " + formBean.getImgFlag();
            }
            if (formBean.getHotActivities() != null && !"".equals(formBean.getHotActivities())) {
                sql += " and n.hotActivities = " + formBean.getHotActivities();
                countSql += " and n.hotActivities = " + formBean.getHotActivities();
            }
            if (formBean.getStartTime() != null && !"".equals(formBean.getStartTime())) {
                sql += " and n.createDate >= '" + formBean.getStartTime() + " 00:00:00'";
                countSql += " and n.createDate >= '" + formBean.getStartTime() + " 00:00:00'";
            }
            if (formBean.getEndTime() != null && !"".equals(formBean.getEndTime())) {
                sql += " and n.createDate <= '" + formBean.getEndTime() + " 23:59:59'";
                countSql += " and n.createDate <= '" + formBean.getEndTime() + " 23:59:59'";
            }
            if (formBean.getType()==3) {
                sql += " and n.categoryFlag = 1";
                countSql += " and n.categoryFlag = 1";
            }
            if (formBean.getType()==4 ||formBean.getType()==5) {
                sql += " and categoryFlag = 0 and categoryid <> -1";
                countSql += " and categoryFlag = 0 and categoryid <> -1";
            }
            if (formBean.getType()==6) {
                sql += " and ImgFlag = 1 ";
                countSql += " and ImgFlag = 1 ";
            }
        }
        if (page != null && row != null) {
            int snum = Integer.parseInt(page) - 1;
            sql += " order by weights desc,createDate desc   limit  " + (snum * Integer.parseInt(row)) + "," + row + " ";
        }
        List<TgInformation> list = dao.findList(sql);
        List<TgInformation> alist = new ArrayList<TgInformation>();
        for (TgInformation tgAnnouncement : list) {
            TgInformation bean = new TgInformation();
            bean.setBrandId(tgAnnouncement.getBrandId());
            bean.setCategoryId(tgAnnouncement.getCategoryId());
            if (tgAnnouncement.getContent() != null) {
                bean.setContent(tgAnnouncement.getImgUrl() + "--" + tgAnnouncement.getContent());
            }
            else {
                bean.setContent(tgAnnouncement.getImgUrl());
            }
            bean.setCreatTimeStr(DateTimeUtil.switchDateToString(tgAnnouncement.getCreateDate(), "yyyy-MM-dd HH:mm:ss"));
            bean.setHot(tgAnnouncement.getHot());
            bean.setHotActivities(tgAnnouncement.getHotActivities());
            bean.setId(tgAnnouncement.getId());
            bean.setImgFlag(tgAnnouncement.getImgFlag());
            bean.setPcategoryId(tgAnnouncement.getPcategoryId());
            bean.setTitle(tgAnnouncement.getTitle());
            bean.setIsRecommend(tgAnnouncement.getIsRecommend());
            bean.setWeights(tgAnnouncement.getWeights());
            if(tgAnnouncement.getWeights()==null)tgAnnouncement.setWeights(0);
            alist.add(bean);
        }
        
        int total = 0;
        if (list.size() != 0) {
            total = Integer.parseInt((dao.findObjectArryList(countSql).get(0))[0] + "");
        }
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("list", alist);
        map.put("total", total);
        return map;
    }
    
    public TgInformation read(Long id) {
        return dao.read(id);
    }
    
    public void saveOrUpdate(TgInformation tgInformation) {
        if(tgInformation.getBrandId()==null){
            tgInformation.setWeights(0);
        }
        dao.saveOrUpdate(tgInformation);
    }
    
    public List<TgInformation> findByCategory(Long categoryId, int start, int limit) {
        String sql =
            "select tgi.id,tgi.title,(select tgc.name from t_g_category tgc  where tgi.categoryId=tgc.id) as content,(select tgc.name  from t_g_category tgc  where tgi.pcategoryId=tgc.id) as imgUrl from t_g_information tgi where  tgi.PcategoryId = ?  and hot = '2' and ImgFlag = '2' and hotActivities = '2' order by weights desc,id desc limit ?,?";
        return dao.findList(sql, new Object[] {categoryId, start, limit});
    }
    
    public List<TgInformation> findByCategoryTask(Long categoryId, int start, int limit) {
        String sql =
            "select tgi.id,tgi.title,(select tgc.name from t_g_category tgc  where tgi.categoryId=tgc.id) as content,(select tgc.name  from t_g_category tgc  where tgi.pcategoryId=tgc.id) as imgUrl from t_g_information tgi where  tgi.PcategoryId = ? and  categoryFlag = 1 and hot = '2' and ImgFlag = '2' and hotActivities = '2' order by weights desc,id desc limit ?,?";
        return dao.findList(sql, new Object[] {categoryId, start, limit});
    }
    
    public List<TgInformation> findByCategoryChild(Long categoryId, int start, int limit) {
        String sql =
            "select tgi.id,tgi.title,tgc.name as content from t_g_information tgi, t_g_category tgc where tgi.categoryId=tgc.id and tgi.categoryId = ? and hot = '2' and ImgFlag = '2' and hotActivities = '2' order by weights desc,createDate desc limit ?,?";
        return dao.findList(sql, new Object[] {categoryId, start, limit});
    }
    
    public List<TgInformation> findByCategoryChild2(Long categoryId, int start, int limit) {
        String sql =
            "select tgi.id,tgi.title,tgc.name as content from t_g_information tgi, t_g_category tgc where tgi.categoryId=tgc.id and tgi.categoryId = ? and hot = '2' and ImgFlag = '2' and categoryFlag = 0 and hotActivities = '2' order by weights desc,createDate desc limit ?,?";
        return dao.findList(sql, new Object[] {categoryId, start, limit});
    }
    
    public List<TgInformation> findByCategoryHot(Long categoryId, int start, int limit) {
        String sql =
            "select tgi.id,tgi.title,tgc.name as content from t_g_information tgi, t_g_category tgc where tgi.categoryId=tgc.id and tgi.PcategoryId = ? and hot = '1' and tgi.ImgFlag = '2' and tgi.hotActivities = '2' order by createDate desc limit ?,?";
        return dao.findList(sql, new Object[] {categoryId, start, limit});
    }
    
    public List<TgInformation> findHotImgInfo() {
        String sql =
            "select tgi.id,tgi.imgUrl,tgc.name as content from t_g_information tgi, t_g_category tgc where tgi.categoryId=tgc.id and hot = '1' and tgi.ImgFlag = '1' and tgi.hotActivities = '2'  order by createDate desc limit ?,?";
        return dao.findList(sql, new Object[] {0, 7});
    }
    
    public List<TgInformation> findImgInfo() {
        String sql =
            "select tgi.id,tgi.imgUrl from t_g_information tgi where tgi.ImgFlag = '1' order by createDate desc limit ?,?";
        return dao.findList(sql, new Object[] {0, 4});
    }
    
    public List<TgInformation> findHotActivity() {
        String sql =
            "select tgi.id,tgi.imgUrl from t_g_information tgi where hotActivities = '1'  order by createDate desc limit 0,3";
        return dao.findList(sql);
    }
    
    public PageUtil findByCategoryId(Long categoryId, int page, int limit) {
        String sql =
            "select tgi.id,tgi.title,tgc.name as content   from t_g_information tgi, t_g_category tgc where tgi.categoryId=tgc.id ";
        String countSql = "select count(*) from t_g_information where 1 =1 ";
        Long tempId = null;
        if (null != categoryId && categoryId > 0l) {
            tempId = categoryId;
            sql += " and (pcategoryid  = ? or categoryId = ?) ";
            countSql += " and (pcategoryid  = ? or categoryId = ?) ";
        }
        sql += " order by createDate desc limit ?,? ";
        List<TgInformation> information = dao.findList(sql, new Object[] {tempId, tempId, (page - 1) * limit, limit});
        PageUtil pu = new PageUtil();
        pu.setPageNo(page);
        pu.setList(information);
        pu.setPageSize(limit);
        List<Object[]> countList = dao.findObjectArryList(countSql, new Object[] {tempId, tempId});
        if (null != countList && countList.size() > 0) {
            Object[] countAry = countList.get(0);
            pu.setTotalRecords(new Integer(countAry[0].toString()));
        }
        return pu;
    }
    
    public List<TgInformation> findHotInfomation() {
        String sql =
            "select * from t_g_information where hot = '1' and  ImgFlag = '2' order by createDate desc limit 0,3";
        return dao.findList(sql);
    }
    
    public boolean deleteByMessId(String id) {
        boolean isFlag = true;
        String sql = "delete from t_g_information where messageId = " + id;
        try {
            dao.delete(sql);
        }
        catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return isFlag;
    }
    
    public TgInformation readByMessId(String id) {
        String sql = "select * from t_g_information where messageId = " + id;
        List<TgInformation> list = dao.findList(sql);
        if (list.isEmpty())
            return null;
        return list.get(0);
    }
    
    public Integer findCategoryInfoCount(Long categroyId) {
        Integer count = 0;
        String sql =
            "select count(tgi.id) from t_g_information tgi, t_g_category tgc where tgi.categoryId=tgc.id and tgi.categoryId = ? and hot = '2' and ImgFlag = '2' and hotActivities = '2' ";
        List<Object[]> countList = dao.findObjectArryList(sql, new Object[] {categroyId});
        if (null != countList && countList.size() > 0) {
            Object[] countAry = countList.get(0);
            count = new Integer(countAry[0].toString());
        }
        return count;
    }
    
    public void modifyGoodsWeight(Long infoId, Integer weights) {
        String sql = "update t_g_information set weights = ? where id = ?";
        dao.update(sql, new Object[] { weights, infoId });
    }

    public TgInformation readBycategoryFlag(int categoryFlag) {
            String sql = "select * from t_g_information where categoryFlag = " + categoryFlag;
            List<TgInformation> list = dao.findList(sql);
            if (list.isEmpty())
                return null;
            return list.get(0);
    }
}
